import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Goomba from Super Mario Bros.
 */
public class Goomba  extends Mover implements Runnable
{
    private GreenfootImage[] walk;
    private retroWorld theWorld;
    private int a=0;
    private int vSpeed; //Current falling speed
    private int acceleration; //Falling speed
    private int halfImg ; //Coordinates of Goomba's feet
    private int halfImgWidth;
    private double moveAmount;
    private boolean alive;
    
    Simon simon;
    Health health;
  
    private volatile Thread t;

    public Goomba()
    {
        alive=true;
        
        walk = new GreenfootImage[4];
        retroWorld theWorld;
        a = 0;
        vSpeed = 0; //Current falling speed
        acceleration = 1; //Falling speed
        halfImg = getImage().getHeight()/2; //Coordinates of Goomba's feet
        halfImgWidth = getImage().getWidth()/2;
        moveAmount = -3.5;
    
    
        walk[0] = new GreenfootImage("Coin1_32.png");
        walk[1] = new GreenfootImage("Coin2_32.png"); 
        walk[2] = new GreenfootImage("Coin3_32.png");
        walk[3] = new GreenfootImage("Coin4_32.png"); 
    }
    
    public void run()
    {
        while(alive)
        {
            if (!alive)
                break;
                
            move(moveAmount);
            fall();
            cycleImage();
            checkCollision();
           
            if(alive)
                if (getX() < 5 || (getX() >= theWorld.worldWidth - 1)){
                    removeFromWorld(); //Remove object if it goes off the screen   
                }
            
            if(alive)
                if (theWorld.isGround(getX() - halfImgWidth, getY()))
                    moveAmount = 3.5;
                
            if(alive)
                if (theWorld.isGround(getX() + halfImgWidth, getY()))
                    moveAmount = -3.5;
                
            if (alive)        
                if (getY() > (theWorld.worldHeight - 60))
                    removeFromWorld();
                
            try {
                t.sleep(20);
            } catch (InterruptedException e) {}
        }
    }
    
    public void addedToWorld(World world)
    {
        theWorld = (retroWorld)world;
        simon = theWorld.getSimon();
        health = theWorld.getHealth();
  
        t = new Thread(this);
        t.start();
    }
    
    private void cycleImage()
    {
        switch(a){
                case 0:
                    setImage(walk[a++]);
                    break;
                case 1:
                    setImage(walk[a++]);
                    break;
                case 2:
                    setImage(walk[a++]);
                    break;
                case 3:
                    setImage(walk[a]);
                    a=0;
                    break;
                 }
    }
    
    private synchronized void checkCollision()
    {
        if (intersects(simon))
        {
            if (simon.isCurrentlyWhipping())
                removeFromWorld();
            else if (simon.isCurrentlyWhipping() == false){
                health.life();
                removeFromWorld();
            }
        }
    }
    
    public void removeFromWorld()
    {
        try {
        
            //setImage("deadGoomba.png");
            t.sleep(100);
            
        } catch (InterruptedException e) {}
        t=null;
        alive=false;
        theWorld.removeObject(this);
    }
    
    private void fall()
    {
        vSpeed = vSpeed + acceleration;
        
        //detect collision with ground pixel by pixel
        for (int i = 0; i <= vSpeed; i++)
        {
            if (alive && theWorld.isGround(getX(), getY() + halfImg + i))
            {
                vSpeed = 0;
                setLocation(getX(), getY() + i);
            }

        }
        
        setLocation (getX(), (getY() + vSpeed));
    }
}